<template>

  <a-modal :label-col="{style: { width: '200px' }}"
           v-model:visible="visible"
           okText="提交"
           cancelText="取消"
           @ok="submit"
           @cancel="emit('close')"
           title="用户登录">
    <p>您的账户已经存在于系统中，请在下面的输入框中输入您的密码，如果您并未注册本网站，请更换邮箱重试，或者联系管理员！</p>
    <a-input-password placeholder="请输入密码" v-model:value="password" />
  </a-modal>
</template>

<script setup>
import {ref} from "vue";
import {apiGet, apiPost} from "../../../../../fentch";
import {message} from "ant-design-vue";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false
  },
  email: {
    type: String,
    required: true,
    default: ''
  }
})

const password = ref('')

const emit = defineEmits(['close', 'confirm'])

const submit = () => {
  apiPost('/auth/login', {
    email: props.email,
    password: password.value
  }).then(res => {
    if (res.data.code === 0) {
      window.localStorage.setItem('token', res.data.data.access_token)
      let now = new Date();
      now.setSeconds(now.getSeconds() + res.data.data.expires_in)
      window.localStorage.setItem('expireTime', now.getTime().toString())
      apiGet('/auth/me').then(res => {
        if (res.data.code === 0) {
          window.localStorage.setItem('user', JSON.stringify(res.data.data))
          emit('confirm')
          return
        }
        message.error(res.data.message)
      })
      return
    }
    message.error(res.data.message)
  })
  emit('close')
}
</script>

<style scoped>

</style>